package com.hq.fxh.No5_User.User.Activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hq.fxh.CustomerView.switchbutton.SwitchButton;
import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.No0_ChengQu.Activity.HomeActivity;
import com.hq.fxh.No0_ChengQu.Login.LoginActivity;
import com.hq.fxh.R;
import com.hq.fxh.Utils.DataCleanManager;
import com.hq.fxh.Utils.ScreenManager;
import com.hq.fxh.Utils.SharedPreferenceUtils;

import cn.jpush.im.android.api.JMessageClient;

public class SettingActivity extends BaseActivity implements View.OnClickListener {

    protected ImageView toolbarImageBack;
    protected LinearLayout toolbarLayoutBack;
    protected TextView toolBarTvTitle;
    protected TextView toolBarTvMore;
    protected LinearLayout toolbarLayoutMore;
    protected TextView tvClear;
    protected RelativeLayout layoutSettingMessageSetting;
    protected RelativeLayout layoutSettingClear;
    protected RelativeLayout layoutSettingBlackList;
    protected RelativeLayout layoutSettingSengLiuLiangMode;
    protected RelativeLayout layoutSettingHaoPing;
    protected RelativeLayout layoutSettingFanKui;
    protected RelativeLayout layoutSettingAboutMe;
    protected SwitchButton sbSwitch;
    protected LinearLayout toolbarContent;
    protected TextView setingLogin;
    int memberId = 0;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_settind;
    }

    @Override
    protected void initView() {
        memberId = SharedPreferenceUtils.getMemberId(this);

        toolbarImageBack = (ImageView) findViewById(R.id.toolbar_image_back);
        toolbarLayoutBack = (LinearLayout) findViewById(R.id.toolbar_Layout_back);
        toolbarLayoutBack.setOnClickListener(SettingActivity.this);
        toolBarTvTitle = (TextView) findViewById(R.id.toolBar_tv_Title);
        toolBarTvMore = (TextView) findViewById(R.id.toolBar_tv_More);
        toolbarLayoutMore = (LinearLayout) findViewById(R.id.toolbar_Layout_More);
        tvClear = (TextView) findViewById(R.id.tv_clear);
        layoutSettingMessageSetting = (RelativeLayout) findViewById(R.id.layout_Setting_MessageSetting);
        layoutSettingMessageSetting.setOnClickListener(SettingActivity.this);
        layoutSettingClear = (RelativeLayout) findViewById(R.id.layout_Setting_Clear);
        layoutSettingClear.setOnClickListener(SettingActivity.this);
        layoutSettingBlackList = (RelativeLayout) findViewById(R.id.layout_Setting_BlackList);
        layoutSettingBlackList.setOnClickListener(SettingActivity.this);
        layoutSettingSengLiuLiangMode = (RelativeLayout) findViewById(R.id.layout_Setting_SengLiuLiangMode);
        layoutSettingSengLiuLiangMode.setOnClickListener(SettingActivity.this);
        layoutSettingHaoPing = (RelativeLayout) findViewById(R.id.layout_Setting_HaoPing);
        layoutSettingHaoPing.setOnClickListener(SettingActivity.this);
        layoutSettingFanKui = (RelativeLayout) findViewById(R.id.layout_Setting_FanKui);
        layoutSettingFanKui.setOnClickListener(SettingActivity.this);
        layoutSettingAboutMe = (RelativeLayout) findViewById(R.id.layout_Setting_AboutMe);
        layoutSettingAboutMe.setOnClickListener(SettingActivity.this);
        sbSwitch = (SwitchButton) findViewById(R.id.sbSwitch);
        toolbarContent = (LinearLayout) findViewById(R.id.toolbar_Content);
        setingLogin = (TextView) findViewById(R.id.seting_login);
        setingLogin.setOnClickListener(SettingActivity.this);
    }

    @Override
    protected void initData() {

        toolBarTvMore.setVisibility(View.INVISIBLE);
        toolBarTvTitle.setText("设置");
        try {
            tvClear.setText(DataCleanManager.getTotalCacheSize(SettingActivity.this) + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.layout_Setting_MessageSetting) {
            clickMessageSet();

        } else if (view.getId() == R.id.layout_Setting_Clear) {
            clickClear();

        } else if (view.getId() == R.id.layout_Setting_BlackList) {
            clickBlackList();
        } else if (view.getId() == R.id.layout_Setting_HaoPing) {
            openApplicationMarket(getApplication().getPackageName());


        } else if (view.getId() == R.id.layout_Setting_FanKui) {
            clickFanKui();

        } else if (view.getId() == R.id.layout_Setting_AboutMe) {
            startActivity(new Intent(SettingActivity.this, AboutActivity.class));

        } else if (view.getId() == R.id.toolbar_Layout_back) {
            finish();
        } else if (view.getId() == R.id.seting_login) {

            clickLogin();

            startActivity(new Intent(SettingActivity.this, LoginActivity.class));

        }
    }

    private void clickLogin() {
        if (memberId!=0){
            saveMemberId(0,null,0,0,null,0,null);
            JMessageClient.logout();
        }
    }


    public void saveMemberId(int memberId, String account, int is_active, int level, String expire_dt, int isCompany, String token) {
        SharedPreferences mySharedPreferences = getSharedPreferences("memberId", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.putInt("memberId", memberId);
        editor.putString("account", account);
        editor.putInt("is_active", is_active);
        editor.putInt("level", level);
        editor.putInt("isCompany", isCompany);
        editor.putString("expire_dt", expire_dt);
        editor.putString("token", token);
        editor.commit();

    }


    private void clickFanKui() {
        startActivity(new Intent(SettingActivity.this, AboutActivity.class));

    }

    private void clickBlackList() {
        startActivity(new Intent(SettingActivity.this, BlackListActivity.class));


    }

    private void clickMessageSet() {
        startActivity(new Intent(SettingActivity.this, MessageSetActivity.class));


    }

    private void clickClear() {
        new AlertDialog.Builder(SettingActivity.this).setTitle("系统提示")//设置对话框标题
                .setMessage("确定要删除数据吗？")//设置显示的内容
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加确定按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                        DataCleanManager.cleanApplicationData(SettingActivity.this);
                        try {
                            tvClear.setText(DataCleanManager.getTotalCacheSize(SettingActivity.this) + "");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加返回按钮
            @Override

            public void onClick(DialogInterface dialog, int which) {//响应事件
            }
        }).show();//在按键响应事件中显示此对话框
    }


    private void openApplicationMarket(String packageName) {
        try {
            String str = "market://details?id=" + packageName;
            Intent localIntent = new Intent(Intent.ACTION_VIEW);
            localIntent.setData(Uri.parse(str));
            startActivity(localIntent);
        } catch (Exception e) {
            // 打开应用商店失败 可能是没有手机没有安装应用市场
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "打开应用商店失败", Toast.LENGTH_SHORT).show();
            // 调用系统浏览器进入商城
            String url = "http://app.mi.com/detail/163525?ref=search";
            openLinkBySystem(url);
        }
    }

    /**
     * 调用系统浏览器打开网页
     *
     * @param url 地址
     */


    private void openLinkBySystem(String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        startActivity(intent);
    }


}
